<template>
  <List ref="qmpList" v-model:finished="finished" :reset-fun="resetData" :load="load" :refresh="load">
    <Item
      v-for="(item, i) in list"
      :key="i"
      :item-data="item"
      :dictData="dictData"
      @detail="toDetail(item)"
      @edit="toEdit(item)"
      @enable="onEnable(item)"
      @disable="toDisable(item)"
    />
  </List>
</template>
<script lang="ts">
  import { ref, reactive, toRefs, defineComponent, watch, nextTick, getCurrentInstance } from 'vue'
  import { useRouter } from 'vue-router'
  import List from '@/components/List/index.vue'
  import Item from './Item.vue'
  import { geList, detailItem, enableItem } from '@/apis/attractInvestment/blankMarket'
  import { getDictByPatchType } from '@/apis/dataDict'

  interface datasModle {
    list: any[]
    finished: boolean
    dictData: any
  }

  export default defineComponent({
    name: 'BlankMarketList',
    components: { List, Item },
    props: {
      searchValue: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        default: ''
      }
    },
    setup(props) {
      const { proxy }: any = getCurrentInstance()

      const qmpList = ref()
      const $router = useRouter()
      const datas = reactive<datasModle>({
        list: [],
        finished: false,
        dictData: {}
      })
      const load = async (page: number, rows: number) => {
        const filtersRaw: object[] = []
        if (props.searchValue) {
          filtersRaw.push({
            id: 'marketName',
            property: 'marketName',
            operator: 'like',
            value: props.searchValue
          })
        }
        const paramsData = {
          page,
          rows,
          sort: '',
          order: 'desc',
          filtersRaw,
          paramsRaw: {},
          sortersRaw: [
            { id: 'enable', property: 'enable', direction: 'DESC' },
            { id: 'isAttractInvestment', property: 'isAttractInvestment', direction: 'ASC' },
            { id: 'createTime', property: 'createTime', direction: 'DESC' }
          ],
          pageFlag: true
        }
        return geList(paramsData).then((res: any) => {
          const resData = res.data
          const list = resData.records
          if (page === 1) {
            datas.list = list
          } else {
            datas.list = datas.list.concat(list)
          }
          if (resData.total <= datas.list.length) {
            datas.finished = true
          }
        })
      }
      const resetData = () => {
        datas.list = []
      }
      const resetPageData = () => {
        qmpList.value.resetPageData()
      }

      const refreshItem = (id: string) => {
        const flag = datas.list.some((v) => v.id === id)
        if (!flag) return

        detailItem(id).then((res: any) => {
          const d = res.data
          if (d && d.id) {
            datas.list = datas.list.map((item: any) => {
              if (item.id === id) {
                item = { ...res.data }
              }
              return item
            })
          }
        })
      }

      // 获取数据字典
      const getDictData = async () => {
        const dictTypeCodes = ['MARKET_NATURE', 'CITY_LEVEL', 'CTM_MARKET_DATA_FROM']
        datas.dictData = await getDictByPatchType({ dictTypeCodes })
      }

      getDictData()

      const toDetail = (row: any) => {
        $router.push({
          path: '/blankMarket/detail',
          query: {
            id: row.id
          }
        })
      }
      const toEdit = (row: any) => {
        $router.push({
          path: '/blankMarket/edit',
          query: {
            id: row.id
          }
        })
      }

      // 启用
      const onEnable = (row: any) => {
        enableItem([row.id])
          .then((res: any) => {
            if (res.success === 1) {
              proxy.$toast(`启用成功`)

              nextTick(() => {
                resetPageData()
              })
            }
          })
          .catch((e: any) => {
            if (e) proxy.$toast(e || '启用失败')
          })
      }
      // 停用
      const toDisable = (row: any) => {
        $router.push({
          path: '/blankMarket/disable',
          query: {
            id: row.id
          }
        })
      }

      watch(
        () => props.searchValue,
        () => {
          resetPageData()
        }
      )
      return {
        ...toRefs(datas),
        load,
        resetData,
        qmpList,
        resetPageData,
        refreshItem,
        toDetail,
        toEdit,
        onEnable,
        toDisable
      }
    }
  })
</script>
<style lang="less" scoped></style>
